package com.shop.loan.service.impl;

import com.alibaba.fastjson.JSON;
import com.shop.loan.mapper.AppHomeSettingByMybatisMapper;
import com.shop.loan.mapper.NoticeMybatisMapper;
import com.shop.loan.model.common.Code;
import com.shop.loan.model.common.ResResult;
import com.shop.loan.model.*;
import com.shop.loan.model.entity.AppHomeSettingByMybatis;
import com.shop.loan.model.entity.ClassifyByMybatis;
import com.shop.loan.model.entity.GoodsByMybatis;
import com.shop.loan.model.entity.SettingByMybatis;
import com.shop.loan.model.vo.AppHomeVo;
import com.shop.loan.model.vo.GoodsVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.redis.RedisUtil;
import com.shop.loan.service.AppHomeService;
import com.shop.loan.service.ChannelService;
import com.shop.loan.service.GoodsService;
import com.shop.loan.service.SettingService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.jpa.dao.EruptDao;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class AppHomeServiceImpl implements AppHomeService {

    @Resource
    GoodsService goodsService;

    @Resource
    SettingService settingService;

    @Resource
    AppHomeSettingByMybatisMapper appHomeSettingByMybatisMapper;

    @Resource
    NoticeMybatisMapper noticeMybatisMapper;

    @Resource
    EruptDao eruptDao;

    @Resource
    RedisUtil redisUtil;

    @Resource
    ChannelService channelService;

    @Override
    public ResResult home(GoodsVo homeVo) {
        String lockKey = RedisCache.appHomeData;
        if (homeVo.getOpId() != null && homeVo.getOpId() > 0) {
            lockKey = RedisCache.appHomeData + "_" + homeVo.getOpId();
        } else if (StringUtils.hasLength(homeVo.getChannelCode())) {
            lockKey = RedisCache.appHomeData + "_" + homeVo.getChannelCode();
        }

        String homeData = redisUtil.get(lockKey);
        if (StringUtils.hasLength(homeData)) {
            return new ResResult(Code.SUCCESS, JSON.parseObject(homeData, AppHomeVo.class));
        }

        homeVo.setChannelShow(false);
        if (homeVo.getOpId() != null && homeVo.getOpId() > 0) {
            User rentalUser = eruptDao.queryEntity(User.class, "id = :id", new HashMap<String, Object>(1) {{
                this.put("id", homeVo.getOpId());
            }});
            if (rentalUser != null && rentalUser.getChannel() != null && rentalUser.getChannel().getId() > 1) {
                homeVo.setChannelShow(true);
            }
        } else if (StringUtils.hasLength(homeVo.getChannelCode())) {
            Channel channel = channelService.getChannelByCode(homeVo.getChannelCode());
            if (channel != null && channel.getId() > 0) {
                homeVo.setChannelShow(true);
            }
        }

        AppHomeVo appHomeVo = new AppHomeVo();
        //首页页面设置
        List<AppHomeSettingByMybatis> appHomeSettingByMybatis = appHomeSettingByMybatisMapper.selectHomeList(homeVo.getChannelShow());
        for (AppHomeSettingByMybatis appHomeSettingByMybati : appHomeSettingByMybatis) {
            if (appHomeSettingByMybati.getJumpType() == null || appHomeSettingByMybati.getClassifyId() == null) {
                continue;
            }
            if (appHomeSettingByMybati.getJumpType().trim().equals("classify")) {
                Classify classify = eruptDao.queryEntity(Classify.class, "id = :id", new HashMap<String, Object>(1) {{
                    this.put("id", appHomeSettingByMybati.getClassifyId());
                }});
                appHomeSettingByMybati.setClassify(classify);
            }
        }
        appHomeVo.setHomeInfo(appHomeSettingByMybatis);

        //热门商品
        GoodsVo goodsVo = new GoodsVo();
        goodsVo.setIsHot(1);
        goodsVo.setPageNum(1);
        goodsVo.setPageSize(50);
        goodsVo.setChannelShow(homeVo.getChannelShow());
        List<GoodsByMybatis> goodsList = goodsService.list(goodsVo).getDataList();
        for (GoodsByMybatis goods : goodsList) {
            goods.setDetail(null);
        }
        appHomeVo.setHotGoods(goodsList);

        //热门榜单
        List<ClassifyByMybatis> classifyByMybatisList = new ArrayList<>();
        ClassifyByMybatis classifyByMybatis = new ClassifyByMybatis();
        if (goodsList != null && goodsList.size() > 5) {
            classifyByMybatis.setGoodsList(goodsList.subList(0, 5));
        } else {
            classifyByMybatis.setGoodsList(goodsList);
        }

        classifyByMybatisList.add(classifyByMybatis);

        appHomeVo.setTopClassify(classifyByMybatisList);
        appHomeVo.setNotices(noticeMybatisMapper.selectByList());

        SettingByMybatis setting = settingService.getSetting();

        PlatformSetting platformSetting = new PlatformSetting();
        platformSetting.setFileDomain(setting.getFileDomain());
        appHomeVo.setPlatformSetting(platformSetting);
        redisUtil.setex(lockKey, 60 * 10, JSON.toJSONString(appHomeVo));
        return new ResResult(Code.SUCCESS, appHomeVo);
    }
}
